/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.util;

import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author yuhuibear
 */
public abstract class StringBufferTool {

	public static void appendWithGap(StringBuffer sb, List<String> lst,
			String gap) {
		if (lst == null) { return; }
		Iterator<String> itr = lst.iterator();
		while (itr.hasNext()) {
			sb.append(itr.next().trim());
			sb.append(gap);
		}
		if (sb.toString().endsWith(gap)) {
			sb.delete(sb.length() - gap.length(), sb.length());

		}
	}

	public static void appendWithGap(StringBuffer sb, String[] strs, String gap) {
		if (strs == null) { return; }
		for (int i = 0; i < strs.length; i++) {
			sb.append(strs[i].trim());
			sb.append(gap);
		}
		if (sb.toString().endsWith(gap)) {
			sb.delete(sb.length() - gap.length(), sb.length());

		}
	}

	public static String appendWithGap(StringBuffer sb, String[] strs,
			String gap, String lastReplacement) {
		if (strs == null) { return null; }
		StringBuffer tsb = new StringBuffer();
		for (int i = 0; i < strs.length; i++) {
			tsb.append(strs[i].trim());
			tsb.append(gap);
		}
		if (tsb.toString().endsWith(lastReplacement)) {
			tsb.delete(tsb.length() - lastReplacement.length(), tsb.length());
		}
		sb.append(tsb);
		return tsb.toString();
	}

	public static void appendWithGap(StringBuffer sb, String str, int rpt,
			String gap, String lastReplacement) {
		if (str == null) { return; }
		for (int i = 0; i < rpt; i++) {
			sb.append(str.trim());
			sb.append(gap);
		}
		if (sb.toString().endsWith(lastReplacement)) {
			sb.delete(sb.length() - lastReplacement.length(), sb.length());

		}
	}
}
